package lzm.utils;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Doc2Pdf {
 
	/*public static void main(String[] args) throws Exception {
		doc2pdf("D:/联调问题记录.docx", "D:/new.pdf");
	}*/
 
	public static boolean getLicense() throws Exception {
		boolean result = false;
		try {
			InputStream inputStream = Doc2Pdf.class.getResourceAsStream("/license/license.xml");
			License aposeLic = new License();
            //System.out.println("license code");
            //System.out.println(IOUtils.toString(inputStream, "utf-8"));
			aposeLic.setLicense(inputStream);
			result = true;
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return result;
	}


	public static long pdf2excel(String inPath, String outPath) throws Exception {
		if (!getLicense()) {
			// 验证License 若不验证则转化出的pdf文档有水印
			throw new Exception("com.aspose.words lic ERROR!");
		}
		String system = System.getProperty("os.name");

		System.out.println(inPath + " -> " + outPath);
		try {
			long old = System.currentTimeMillis();
			File file = new File(outPath);
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(inPath); // word文档
			// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
			doc.save(os, SaveFormat.TEXT);
			os.flush();
			os.close();
			long now = System.currentTimeMillis();
			System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
			return file.length();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
 
	public static long doc2pdf(String inPath, String outPath) throws Exception {
		if (!getLicense()) { 
			// 验证License 若不验证则转化出的pdf文档有水印
			throw new Exception("com.aspose.words lic ERROR!");
		}
		String system = System.getProperty("os.name"); 
		// windows下和linux下字体放的地方不一样
		if(system.toLowerCase().startsWith("win")){
			// windows默认字体是放在C:/Windows/Fonts下
			FontSettings.setFontsFolder("C:/Windows/Fonts", false);
		} else {
			// linux默认字体是放在/usr/share/fonts下，为了方便将字体放在应用文件夹/home/fssc/Fonts里面
			FontSettings.setFontsFolder("/home/fssc/Fonts", false);
		}
		System.out.println(inPath + " -> " + outPath);
		try {
			long old = System.currentTimeMillis();
			File file = new File(outPath);
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(inPath); // word文档
			// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
			doc.save(os, SaveFormat.PDF);
            os.flush();
            os.close();
			long now = System.currentTimeMillis();
			System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
            return file.length();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}


	public static File doc2pdf(File wordFile, String outPath) throws Exception {
		if (!getLicense()) {
			// 验证License 若不验证则转化出的pdf文档有水印
			throw new Exception("com.aspose.words lic ERROR!");
		}
		String system = System.getProperty("os.name");
		// windows下和linux下字体放的地方不一样
		if(system.toLowerCase().startsWith("win")){
			// windows默认字体是放在C:/Windows/Fonts下
			FontSettings.setFontsFolder("C:/Windows/Fonts", false);
		} else {
			// linux默认字体是放在/usr/share/fonts下，为了方便将字体放在应用文件夹/home/fssc/Fonts里面
			FontSettings.setFontsFolder("/home/fssc/Fonts", false);
		}
		try {
			long old = System.currentTimeMillis();
			File file = new File(outPath);
			FileOutputStream os = new FileOutputStream(file);
			Document doc = new Document(new FileInputStream(wordFile)); // word文档
			// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
			doc.save(os, SaveFormat.PDF);
			os.flush();
			os.close();
			long now = System.currentTimeMillis();
			System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}